//
//  HECartTabBarView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/27.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HECartTabBarView.h"

@interface HECartTabBarView ()

@property (nonatomic, strong) UIImageView *selectBtn;
@property (nonatomic, strong) QMUIFillButton *tobtn;

@property (nonatomic, assign) BOOL allSelect;
@property (nonatomic, assign) NSInteger total; // 总商品数

@property (nonatomic, strong) UILabel *totalLb; // 合计
@property (nonatomic, strong) UILabel *priceLb; // 总价格

@end

@implementation HECartTabBarView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = KWhiteColor;
        
        self.qmui_borderPosition = QMUIViewBorderPositionTop;
        self.qmui_borderWidth = 0.5;
        self.qmui_borderColor = CSplitLineColor;
        self.actionType = HECartTabBarActionTypeSettlement;
        
        _total = 0;
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    
    UIView *selectWarp = [UIView new];
    [self addSubview:selectWarp];
    
    // 勾选
    _selectBtn = [UIImageView new];
    _selectBtn.image = IMAGE_NAMED(@"check_box_unsel");
    _selectBtn.userInteractionEnabled = YES;
    [selectWarp addSubview:_selectBtn];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectGoodsClick)];
    [selectWarp addGestureRecognizer:tap];
    
    // 全选
    UILabel *allLb = [UILabel new];
    allLb.text = @"全选";
    allLb.font = DYFont(16);
    [selectWarp addSubview:allLb];
    
    [selectWarp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.left.equalTo(self).mas_offset(15);
        make.height.mas_offset(55);
     }];
        
    // 合计
     UILabel *totalLb = [UILabel new];
     _totalLb = totalLb;
     totalLb.text = @"合计：";
     totalLb.font = DYFont(16);
     [self addSubview:totalLb];
    
    _priceLb = [UILabel new];
    _priceLb.text = @"￥0.00";
    _priceLb.font = DYBlodFont(16);
    _priceLb.textColor = CPriceColor;
    [self addSubview:_priceLb];
    
    // 结算
    _tobtn = [[QMUIFillButton alloc] initWithFillType:QMUIFillButtonColorBlue];
    _tobtn.titleLabel.font = DYBlodFont(15);
    [_tobtn setBackgroundColor:CNavBgColor];
    [_tobtn setTitle:@"去结算(0)" forState:UIControlStateNormal];
    [self addSubview: _tobtn];
    [_tobtn addTarget:self action:@selector(toSettlementClick) forControlEvents:UIControlEventTouchUpInside];
    
    [_tobtn mas_makeConstraints:^(MASConstraintMaker *make) {
          make.top.equalTo(self).mas_offset(8);
          make.right.equalTo(self).mas_offset(-15);
          make.width.mas_offset(110);
          make.height.mas_offset(39);
    }];
    
    [_selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectWarp);
        make.left.equalTo(self).mas_offset(15);
        make.height.width.mas_offset(25);
    }];

    [allLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selectWarp);
        make.left.equalTo(_selectBtn.mas_right).mas_offset(6);
        make.right.equalTo(selectWarp.mas_right);
    }];
    
    [allLb setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

    [totalLb mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.equalTo(_tobtn);
         make.left.equalTo(selectWarp.mas_right).mas_offset(32);
     }];
    [totalLb setContentHuggingPriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];

    [_priceLb mas_makeConstraints:^(MASConstraintMaker *make) {
         make.centerY.equalTo(_tobtn);
         make.left.equalTo(totalLb.mas_right).mas_offset(0);
         make.right.equalTo(_tobtn.mas_left).mas_offset(-5);
     }];
    [_priceLb setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

}

- (void)selectGoodsClick {
    _allSelect = !_allSelect;
    [self changeSelectBtnImage];
    
    if ([self.delegate respondsToSelector:@selector(tabbarSelectAll:)]) {
        [self.delegate tabbarSelectAll:_allSelect];
    }
}

- (void)toSettlementClick {
    if ([self.delegate respondsToSelector:@selector(tabbarDidSelectWithActionType:)]) {
        [self.delegate tabbarDidSelectWithActionType:_actionType];
    }
}

#pragma mark -
- (void)updateTabDataWithSelect:(BOOL)allSelect totalPrice:(CGFloat)totalPrice total:(NSInteger)total {
    
    _allSelect = allSelect;
    _total = total;
    
    [self changeSelectBtnImage];
    
    _priceLb.text = [NSString stringWithFormat:@"￥%.2f",totalPrice];
    [self changeTotalLb];
}

- (void)changeSelectBtnImage {
    if (_allSelect) {
        _selectBtn.image = IMAGE_NAMED(@"check_box_sel");
    }else {
        _selectBtn.image = IMAGE_NAMED(@"check_box_unsel");
    }
}

- (void)setActionType:(HECartTabBarActionType)actionType {
    _actionType = actionType;
    
    if (actionType == HECartTabBarActionTypeSettlement) {
        _totalLb.hidden = NO;
         _priceLb.hidden = NO;
         _tobtn.fillColor = CNavBgColor;
        [self changeTotalLb];
    }else if (actionType == HECartTabBarActionTypeDelete) {
        _totalLb.hidden = YES;
        _priceLb.hidden = YES;
        [self changeTotalLb];
    }
}

- (void)changeTotalLb {
    
    NSString *str = @"";
    if (_total > 99) {
        str = @"99+";
    }else {
        str = [NSString stringWithFormat:@"%ld", _total];
    }
    
    if (_actionType == HECartTabBarActionTypeSettlement) {
         _tobtn.fillColor = CNavBgColor;
        [_tobtn setTitle:[NSString stringWithFormat:@"结算(%@)", str] forState:UIControlStateNormal];

    }else if (_actionType == HECartTabBarActionTypeDelete) {
        _tobtn.fillColor = KHexColor(@"FF4D4D");
        [_tobtn setTitle:[NSString stringWithFormat:@"删除(%@)", str] forState:UIControlStateNormal];
    }
}

@end
